Attribute VB_Name = "mdlPipe"
Option Explicit

Public Const NORMAL_PRIORITY_CLASS As Long = &H20&
Public Const STARTF_USESTDHANDLES As Long = &H100&
Public Const STARTF_USESHOWWINDOW As Long = &H1&
Public Const SW_HIDE As Long = 0&
Public Const INFINITE As Long = &HFFFF&

Public Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Public Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Public Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Public Declare Function CreateProcess Lib "kernel32.dll" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Public Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CreatePipe Lib "kernel32.dll" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long

Public Function RunCommand(commandLine As String) As String
    On Error Resume Next
    Dim si As STARTUPINFO
    Dim pi As PROCESS_INFORMATION
    Dim retval As Long
    Dim hRead As Long
    Dim hWrite As Long
    Dim sBuffer(0 To 1023) As Byte
    Dim lgSize As Long
    Dim sa As SECURITY_ATTRIBUTES
    Dim strResult As String
    With sa
        .nLength = Len(sa)
        .bInheritHandle = 1&
        .lpSecurityDescriptor = 0&
    End With
    retval = CreatePipe(hRead, hWrite, sa, 0&)
    If retval = 0 Then
        RunCommand = vbNullString
        Exit Function
    End If
    With si
        .cb = Len(si)
        .dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
        .wShowWindow = SW_HIDE
        .hStdOutput = hWrite
    End With
    retval = CreateProcess(vbNullString, commandLine & vbNullChar, sa, sa, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
    If retval Then
        WaitForSingleObject pi.hProcess, INFINITE
        Do While ReadFile(hRead, sBuffer(0), 1024, lgSize, ByVal 0&)
            strResult = strResult & StrConv(sBuffer(), vbUnicode)
            Erase sBuffer()
            If lgSize <> 1024 Then
                Exit Do
            End If
            DoEvents
        Loop
        CloseHandle pi.hProcess
        CloseHandle pi.hThread
    End If
    CloseHandle hRead
    CloseHandle hWrite
    RunCommand = Replace(strResult, vbNullChar, vbNullString)
End Function
